package com.didispace.hystrix;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate;
import rx.Observable;
import rx.Subscriber;

// 第5章  服务容错保护： Spring Cloud Hystrix -- 使用详解 -- 创建请求命令
public class UserObservableCommand extends HystrixObservableCommand<User> {

    private RestTemplate restTemplate;
    private Long id;

    // public UserObservableCommand(Setter setter, RestTemplate restTemplate, Long id){
    //     super(setter);
    //     this.restTemplate = restTemplate;
    //     this.id = id;
    // }

    public UserObservableCommand (RestTemplate restTemplate, Long id) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.restTemplate = restTemplate;
        this.id = id;
    }

    // p153  HystrixObservableCommand
    @Override
    protected Observable<User> construct() {
        return Observable.create(new Observable.OnSubscribe<User>() {
            @Override
            public void call(Subscriber<? super User> observer) {
                try {
                    if (!observer.isUnsubscribed()) {
                        // User user = restTemplate.getForObject("http://USER-SERVICE/users/{1}", User.class, id);
                        User user = restTemplate.getForObject("http://localhost:8081/users", User.class);
                        observer.onNext(user);
                        observer.onCompleted();
                    }
                } catch (Exception e) {
                    observer.onError(e);
                }
            }
        });
    }

    // p154  HystrixObservableCommand
    @HystrixCommand
    public Observable<User> getUserById(final String id) {
        return Observable.create(new Observable.OnSubscribe<User> () {
            @Override
            public void call(Subscriber<? super User> observer) {
                try {
                    if (!observer.isUnsubscribed()) {
                        // User user = restTemplate.getForObject("http://HELLO-SERVICE/users/{1}", User.class, id);
                        User user = restTemplate.getForObject("http://localhost:8081/users", User.class);
                        observer.onNext(user);
                        observer.onCompleted();
                    }
                } catch (Exception e) {
                    observer.onError(e);
                }
            }
        });
    }

    // public static void main(String[] args) {
    //     UserObservableCommand userObservableCommand = new UserObservableCommand(new RestTemplate(),0L);
    //     Observable<User> user = userObservableCommand.construct();
    //     user.subscribe(user1 -> {
    //         System.out.print("[name="+user1.getName()+",");
    //         System.out.println("age="+user1.getAge()+"]");
    //     });
    //
    //     UserObservableCommand userObservableCommand2 = new UserObservableCommand(new RestTemplate(),1L);
    //     Observable<User> user2 = userObservableCommand2.getUserById("1");
    //     user2.subscribe(user1 -> {
    //         System.out.print("[name="+user1.getName()+",");
    //         System.out.println("age="+user1.getAge()+"]");
    //     });
    // }
}

// [name=zhaiyongchao,age=17]
// [name=zhaiyongchao,age=17]

// 18:13:03.193 [main] DEspringframework.web.client.RestTemplate - GET request for "http://localhost:8081/users" resulted in 200 (OK)
// 18:13:03.278 [main] DEBUG org.sBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8081/users"
//         18:13:03.232 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
// 18:13:03.253 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:8081/users" resulted in 200 (OK)
// 18:13:03.254 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class com.didispace.hystrix.User] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@ae13544]
// [name=zhaiyongchao,age=17]
// 18:13:03.273 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:8081/users"
// 18:13:03.274 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
// 18:13:03.278 [main] DEBUG org.pringframework.web.client.RestTemplate - Reading [class com.didispace.hystrix.User] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@707194ba]
// [name=zhaiyongchao,age=17]
